package adapter;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dispatchapp.andlei.R;

import java.util.List;

import bean.OrderListBean;
import utils.Logger;
import utils.SPUtils;
import utils.TimerUtil;
import utils.TxtUtils;

/**
 * @author Andlei
 * @DATE 2019/1/28
 */
public class OrderListAdapter extends BaseQuickAdapter<OrderListBean, BaseViewHolder> {
    public OrderListAdapter(int layoutResId, @Nullable List<OrderListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderListBean item) {
        try {
            helper.setText(R.id.tv_time_name2, item.getAddr_name());
            helper.setText(R.id.tv_order_address, item.getAddress());
            helper.setText(R.id.tv_id, "#" + item.getOrder_num());
            if (TextUtils.isEmpty(item.getRemark())) {
                helper.setText(R.id.tv_order_remark, item.getStore_name());
            } else {
                helper.setText(R.id.tv_order_remark, item.getStore_name() + " (" + item.getRemark() + ")");
            }
            helper.setText(R.id.tv_money, "￥" + item.getMoney());
            String time = item.getTime();
            int end_time = item.getEnd_time();
            //Integer.parseInt(item.getEnd_time());
            TextView textView = helper.getView(R.id.tv_time_name);
            /**
             * 普通订单
             */
            if (end_time == 0) {
                helper.setBackgroundColor(R.id.tv_id, Color.parseColor("#A0F4C9"));
                //1校内骑手  2调度员 3校外骑手 0未指定身份
                switch (item.status) {
                    case "paid":
                    case "received_order":
                    case "take_food":
                        if (SPUtils.getInstance(mContext).getString("Is_coach").equals("1")) {
                            textView.setTextColor(Color.parseColor("#666666"));
                            textView.setCompoundDrawables(null, null, null, null);
                            textView.setEnabled(false);
                            if (!TextUtils.isEmpty(time)) {
                                int mint = Integer.parseInt(time) / 60;
                                if (mint < 600) {
                                    textView.setText(mint + "分钟");
                                } else {
                                    textView.setText(">600分钟");
                                }
                            } else {
                                textView.setText("0分钟");
                            }
                        } else {
                            Drawable drawable = null;
                            if (item.getUsed_second() > 0) {
                                if (item.getUsed_second() / 60 > 10) {
                                    drawable = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);
                                    textView.setTextColor(Color.parseColor("#3AB335"));
                                } else {
                                    drawable = mContext.getResources().getDrawable(R.mipmap.icon_yellow_wen);
                                    textView.setTextColor(Color.parseColor("#FFB949"));
                                }
                                textView.setText("倒计时" + item.getUsed_second() / 60 + "分钟");

                            } else {
                                drawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                                textView.setText("超时" + Math.abs(item.getUsed_second() / 60) + "分钟 (" + Integer.parseInt(time) / 60 + "分钟)");
                                textView.setTextColor(Color.parseColor("#EA1D11"));
                            }
                            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
                            textView.setCompoundDrawables(null, null, drawable, null);
                            textView.setEnabled(true);
                        }
                        break;
                    case "transfer":
                        Drawable drawable = null;
                        if (item.getUsed_second() > 0) {
                            if (item.getUsed_second() / 60 > 10) {
                                drawable = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);
                                textView.setTextColor(Color.parseColor("#3AB335"));
                            } else {
                                drawable = mContext.getResources().getDrawable(R.mipmap.icon_yellow_wen);
                                textView.setTextColor(Color.parseColor("#FFB949"));
                            }
                            textView.setText("倒计时" + item.getUsed_second() / 60 + "分钟");

                        } else {
                            drawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                            textView.setText("超时" + Math.abs(item.getUsed_second() / 60) + "分钟 (" + Integer.parseInt(time) / 60 + "分钟)");
                            textView.setTextColor(Color.parseColor("#EA1D11"));
                        }
                        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
                        textView.setCompoundDrawables(null, null, drawable, null);
                        textView.setEnabled(true);
                        break;
                    case "distributing":
                        Drawable ddrawable = null;
                        if (item.getUsed_second() > 0) {
                            if (item.getUsed_second() / 60 > 10) {
                                ddrawable = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);
                                textView.setTextColor(Color.parseColor("#3AB335"));
                            } else {
                                ddrawable = mContext.getResources().getDrawable(R.mipmap.icon_yellow_wen);
                                textView.setTextColor(Color.parseColor("#FFB949"));
                            }
                            textView.setText("倒计时" + item.getUsed_second() / 60 + "分钟");

                        } else {
                            ddrawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                            textView.setText("超时" + Math.abs(item.getUsed_second() / 60) + "分钟 (" + Integer.parseInt(time) / 60 + "分钟)");
                            textView.setTextColor(Color.parseColor("#EA1D11"));
                        }
                        ddrawable.setBounds(0, 0, ddrawable.getMinimumWidth(), ddrawable.getMinimumHeight());//必须设置图片大小，否则不显示
                        textView.setCompoundDrawables(null, null, ddrawable, null);
                        textView.setEnabled(true);
                        break;
                    case "sark":
                    case "delivery":
                        if (item.getUsed_second() > 0) {
                            if (item.getUsed_second() / 60 > 10) {
                                //drawable = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);
                                textView.setTextColor(Color.parseColor("#3AB335"));
                            } else {
                              //  drawable = mContext.getResources().getDrawable(R.mipmap.icon_yellow_wen);
                                textView.setTextColor(Color.parseColor("#FFB949"));
                            }
                            textView.setText("" + Integer.parseInt(time) / 60 + "分钟");
                        } else {
                           // drawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                            textView.setText("超时" + Math.abs(item.getUsed_second() / 60) + "分钟 (" + Integer.parseInt(time) / 60 + "分钟)");
                            textView.setTextColor(Color.parseColor("#EA1D11"));
                        }
                        //drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
                        textView.setCompoundDrawables(null, null, null, null);
                        textView.setEnabled(false);
                        break;
                    default:
                        if (SPUtils.getInstance(mContext).getString("Is_coach").equals("1")) {
                            textView.setTextColor(Color.parseColor("#666666"));
                            textView.setCompoundDrawables(null, null, null, null);
                            textView.setEnabled(false);
                            if (!TextUtils.isEmpty(time)) {
                                int mint = Integer.parseInt(time) / 60;
                                if (mint < 600) {
                                    textView.setText(mint + "分钟");
                                } else {
                                    textView.setText(">600分钟");
                                }
                            } else {
                                textView.setText("0分钟");
                            }
                        } else {
                            Drawable drawables = null;
                            if (item.getUsed_second() > 0) {
                                if (item.getUsed_second() / 60 > 10) {
                                    drawables = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);
                                    textView.setTextColor(Color.parseColor("#3AB335"));
                                } else {
                                    drawables = mContext.getResources().getDrawable(R.mipmap.icon_yellow_wen);
                                    textView.setTextColor(Color.parseColor("#FFB949"));
                                }
                                textView.setText("倒计时" + item.getUsed_second() / 60 + "分钟");

                            } else {
                                drawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                                textView.setText("超时" + Math.abs(item.getUsed_second() / 60) + "分钟 (" + Integer.parseInt(time) / 60 + "分钟)");
                                textView.setTextColor(Color.parseColor("#EA1D11"));
                            }
                            drawables.setBounds(0, 0, drawables.getMinimumWidth(), drawables.getMinimumHeight());//必须设置图片大小，否则不显示
                            textView.setCompoundDrawables(null, null, drawables, null);
                            textView.setEnabled(true);
                        }
                        break;
                }
//            if (!TextUtils.isEmpty(time)) {
//                int mint = Integer.parseInt(time) / 60;
//                if (mint < 20) {
//                    textView.setText(mint + "分钟");
//                    textView.setTextColor(Color.parseColor("#999999"));
//                } else if (20 <= mint && mint < 40) {
//                    textView.setText(mint + "分钟");
//                    textView.setTextColor(Color.parseColor("#3AB335"));
//                } else if (40 <= mint && mint <= 60) {
//                    textView.setText(mint + "分钟");
//                    textView.setTextColor(Color.parseColor("#FF9F1D"));
//                } else if (60 < mint && mint < 600) {
//                    textView.setText(mint + "分钟");
//                    textView.setTextColor(Color.parseColor("#EA1D11"));
//                } else {
//                    textView.setText(">600分钟");
//                    textView.setTextColor(Color.parseColor("#EA1D11"));
//                }
//            } else {
//                textView.setText("0分钟");
//            }

            }
            /**
             * 预订单
             */
            else {
                int mint = (end_time / 60) + 1;
                helper.setBackgroundColor(R.id.tv_id, Color.parseColor("#FFB949"));
                //1校内骑手  2调度员 3校外骑手 0未指定身份
                switch (item.status) {
                    case "paid":
                    case "received_order":
                    case "take_food":

                        if (SPUtils.getInstance(mContext).getString("Is_coach").equals("1")) {
                            textView.setTextColor(Color.parseColor("#666666"));
                            textView.setCompoundDrawables(null, null, null, null);
                            textView.setEnabled(false);
                            if (end_time > 0) {
                                textView.setText("[预]  " + TxtUtils.addDate(mint) + "送达");
                            } else {
                                if (Math.abs(mint) > 600) {
                                    textView.setText("[预]  已超时>600分钟");
                                } else {
                                    textView.setText("[预]  已超时" + Math.abs(mint) + "分钟");
                                }
                            }
                        } else {
                            Drawable drawable = null;
                            if (item.getUsed_second() >= 0) {
                                if ((item.getUsed_second() - item.time_lag) > 0) {
                                    textView.setText("[预]  " + TxtUtils.addDate(end_time/60) + "送达");
                                    textView.setTextColor(Color.parseColor("#3AB335"));
                                    drawable = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);

                                    if (item.status.equals("transfer")) {
                                        textView.setText("[预]  超时-" + (item.getUsed_second() - item.time_lag) / 60 + "分钟");
                                        textView.setTextColor(Color.parseColor("#EA1D11"));
                                        drawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                                    }
                                } else {
                                    if (item.getUsed_second() / 60 > 10) {
                                        textView.setTextColor(Color.parseColor("#3AB335"));
                                        drawable = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);
                                    } else {
                                        drawable = mContext.getResources().getDrawable(R.mipmap.icon_yellow_wen);
                                        textView.setTextColor(Color.parseColor("#FFB949"));
                                    }
                                    textView.setText("[预]  倒计时" + item.getUsed_second() / 60 + "分钟");
                                }

                            } else {
                                drawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                                textView.setText("[预]  超时" + Math.abs(item.getUsed_second() / 60) + "分钟");
                                textView.setTextColor(Color.parseColor("#EA1D11"));
                            }
                            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
                            textView.setCompoundDrawables(null, null, drawable, null);
                            textView.setEnabled(true);
                        }
                        break;
                    case "transfer":
                        Drawable tdrawable = null;
                        if (item.getUsed_second() >= 0) {
                            if ((item.getUsed_second() - item.time_lag) > 0) {
                                textView.setText("[预]  " + TxtUtils.addDate(end_time/60) + "送达");
                                textView.setTextColor(Color.parseColor("#3AB335"));
                                tdrawable = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);

                                if (item.status.equals("transfer")) {
                                    textView.setText("[预]  超时-" + (item.getUsed_second() - item.time_lag) / 60 + "分钟");
                                    textView.setTextColor(Color.parseColor("#EA1D11"));
                                    tdrawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                                }
                            } else {
                                if (item.getUsed_second() / 60 > 10) {
                                    textView.setTextColor(Color.parseColor("#3AB335"));
                                    tdrawable = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);
                                } else {
                                    tdrawable = mContext.getResources().getDrawable(R.mipmap.icon_yellow_wen);
                                    textView.setTextColor(Color.parseColor("#FFB949"));
                                }
                                textView.setText("[预]  倒计时" + item.getUsed_second() / 60 + "分钟");
                            }

                        } else {
                            tdrawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                            textView.setText("[预]  超时" + Math.abs(item.getUsed_second() / 60) + "分钟");
                            textView.setTextColor(Color.parseColor("#EA1D11"));
                        }
                        tdrawable.setBounds(0, 0, tdrawable.getMinimumWidth(), tdrawable.getMinimumHeight());//必须设置图片大小，否则不显示
                        textView.setCompoundDrawables(null, null, tdrawable, null);
                        textView.setEnabled(true);
                        break;
                    case "distributing":
                        Drawable drawable = null;
                        if (item.getUsed_second() > 0) {
                            if ((item.getUsed_second() - item.time_lag) > 0) {
                                textView.setText("[预]  " + TxtUtils.addDate(mint) + "送达");
                                textView.setTextColor(Color.parseColor("#3AB335"));
                                drawable = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);

                                if (item.status.equals("delivery")) {
                                    textView.setText("[预]  超时-" + (item.getUsed_second() - item.time_lag) / 60 + "分钟");
                                    textView.setTextColor(Color.parseColor("#EA1D11"));
                                    drawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                                }
                            } else {
                                if (item.getUsed_second() / 60 > 10) {
                                    textView.setTextColor(Color.parseColor("#3AB335"));
                                    drawable = mContext.getResources().getDrawable(R.mipmap.icon_green_wen);
                                } else {
                                    drawable = mContext.getResources().getDrawable(R.mipmap.icon_yellow_wen);
                                    textView.setTextColor(Color.parseColor("#FFB949"));
                                }
                                textView.setText("[预]  倒计时" + item.getUsed_second() / 60 + "分钟");
                            }
                        } else {
                            drawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                            textView.setText("[预]  超时" + Math.abs(item.getUsed_second() / 60) + "分钟");
                            textView.setTextColor(Color.parseColor("#EA1D11"));
                        }
                        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
                        textView.setCompoundDrawables(null, null, drawable, null);
                        textView.setEnabled(true);
                        break;
                    case "sark":
                    case "delivery":
                        if (item.getUsed_second() > 0) {

                            //drawable = null;
                            textView.setTextColor(Color.parseColor("#FFB949"));
                            textView.setText("[预]  " + TimerUtil.timedate(item.getWill_send_time()) + "送达");
                        } else {
                            //drawable = mContext.getResources().getDrawable(R.mipmap.icon_red_wen);
                            textView.setText("[预]  超时" + Math.abs(item.getUsed_second() / 60) + "分钟");
                            textView.setTextColor(Color.parseColor("#EA1D11"));
                        }
                       // drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
                        textView.setCompoundDrawables(null, null, null, null);
                        textView.setEnabled(false);
                        break;
                }


//            if(end_time>0){
//                textView.setText("[预]  "+ TxtUtils.addDate(mint) +"送达");
//              //  textView.setText("[预]  "+ mint +"分钟内送达");
//                helper.setBackgroundColor(R.id.tv_id,Color.parseColor("#FFB949"));
//                textView.setTextColor(Color.parseColor("#FFB949"));
//            }else {
//                if(Math.abs(mint)>600){
//                    textView.setText("[预]  已超时>600分钟");
//                }else {
//                    textView.setText("[预]  已超时"+Math.abs(mint)+"分钟");
//                }
//                helper.setBackgroundColor(R.id.tv_id,Color.parseColor("#FFB949"));
//                textView.setTextColor(Color.parseColor("#EA1D11"));
//            }
            }



            if (item.isSelect) {
                ((ImageView) helper.getView(R.id.img_select)).setImageResource(R.mipmap.icon_order_isselect);
            } else {
                ((ImageView) helper.getView(R.id.img_select)).setImageResource(R.mipmap.iconc_noselect);
            }
            if (item.isGone) {
                helper.getView(R.id.img_select).setVisibility(View.GONE);
            } else {
                helper.getView(R.id.img_select).setVisibility(View.VISIBLE);
            }
            try {
                if (item.getTake_tag().equals("1")) {
                    ((ImageView) helper.getView(R.id.img_is_sure)).setImageResource(R.mipmap.icon_mark_yes);
                } else {
                    ((ImageView) helper.getView(R.id.img_is_sure)).setImageResource(R.mipmap.icon_mark_no);
                }
            } catch (Exception e) {
                Logger.d("getTake_tag()-->", "异常捕获，防止App崩溃");
            }
            if (item.DaoDian) {
                ((ImageView) helper.getView(R.id.img_daodian)).setImageResource(R.mipmap.icon_daodian_is);
            } else {
                ((ImageView) helper.getView(R.id.img_daodian)).setImageResource(R.mipmap.icon_daodian_no);
            }
            //是否隐藏 确认取餐按钮
            if (item.isSureGone) {
                helper.getView(R.id.img_is_sure).setVisibility(View.GONE);
            } else {
                helper.getView(R.id.img_is_sure).setVisibility(View.VISIBLE);
            }
            //是否隐藏 确认到店按钮
            if (item.isDaoDianGone) {
                helper.getView(R.id.img_daodian).setVisibility(View.GONE);
            } else {
                helper.getView(R.id.img_daodian).setVisibility(View.VISIBLE);
            }

            //distribution_type 0快跑送弃用；1商家自配送；2支持外端物流；3仅校内物流
            if (item.getDistribution_type() == 2) {
                helper.setImageResource(R.id.img_ordertype, R.mipmap.icon_inschool_outschool);
            } else if (item.getDistribution_type() == 3) {
                helper.setImageResource(R.id.img_ordertype, R.mipmap.icon_only_inschool);
            } else {
                helper.setImageResource(R.id.img_ordertype, R.color.white);
            }
            if (item.out_arrive_time > 0) {
                helper.setImageResource(R.id.img_daodian, R.mipmap.icon_daodian_is);
            } else {
                helper.setImageResource(R.id.img_daodian, R.mipmap.icon_daodian_no);
            }
            //是否隐藏 商家出餐标识
            if (item.store_cooking_out_time > 0) {
                helper.getView(R.id.img_cooking_out).setVisibility(View.VISIBLE);
            } else {
                helper.getView(R.id.img_cooking_out).setVisibility(View.GONE);
            }
            //是否已发送短信
            if(item.getSend_msg() > 0){
                helper.getView(R.id.img_is_send_msg).setVisibility(View.VISIBLE);
            }else {
                helper.getView(R.id.img_is_send_msg).setVisibility(View.GONE);
            }
            helper.addOnClickListener(R.id.tv_time_name);
            helper.addOnClickListener(R.id.img_select);
            helper.addOnClickListener(R.id.img_phone);
            helper.addOnClickListener(R.id.img_is_sure);
            helper.addOnClickListener(R.id.img_daodian);
        } catch (Exception e) {
            Logger.e("OrderListAdapter--->", "防止数据适配器为空异常闪退");
        }
    }
}
